﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Chaos.messages
{
    class request_assign_factions_randomly : message
    {
        public override void process(context in_context)
        {
            RandomlyAssignFactionEvent e = new RandomlyAssignFactionEvent();
            System.Windows.Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                new OnRandomlyAssignFactionDelegate(OnRandomlyAssignFactionUIThread), e);
        }

        public override void serialize(stream.stream in_Stream)
        {
            in_Stream.begin("request_assign_factions_randomly")
            .end();
        }

        public class RandomlyAssignFactionEvent : EventArgs
        {
            public RandomlyAssignFactionEvent()
            {
            }
        };
        public delegate void OnRandomlyAssignFactionDelegate(RandomlyAssignFactionEvent e);

        public void OnRandomlyAssignFactionUIThread(RandomlyAssignFactionEvent e)
        {
            List<String> players = new List<string>();
            List<String> factions = new List<string>();

            model.appinfo appinfo = System.Windows.Application.Current.FindResource("model_appinfo") as model.appinfo;
            if (appinfo != null)
            {
                foreach ( model.player p in appinfo.Players )
                {
                    players.Add(p.Name);
                }
            }
            
            model.game g = System.Windows.Application.Current.FindResource("model_game") as model.game;
            if (g != null)
            {
                foreach (model.faction f in g.Factions)
                {
                    if (f.Locked)
                    {
                        players.Remove(f.User);
                    }
                    else
                    {
                        factions.Add(f.Name);
                    }
                }
            }

            Random r = new Random();

            while(factions.Count > 0 )
            {
                String f = factions[r.Next() % factions.Count];
                factions.Remove(f);
                
                if ( players.Count > 0 )
                {
                    String p = players[r.Next() % players.Count];
                    players.Remove(p);
                    if (g.findfactionbyuser(p) != null )
                    {
                        g.findfactionbyuser(p).User = "";
                    }

                    g.findfactionbyname(f).User = p;
                    
                }
            }

            network.gameserver gs = System.Windows.Application.Current.FindResource("game_server") as network.gameserver;
            if (gs != null)
            {
                process_model_game message = new process_model_game();
                message.Game = g;
                gs.BroadcastMessage(message);
            }
        }
    }
}
